package com.marvels.loanapply.customerservice.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.loanapply.consum.LoanapplyConsumerDubboApi;
import com.marvels.loanapply.consum.LoanapplyConsumerFinancialApi;
import com.marvels.loanapply.customerservice.dto.CustomerServiceDTO;
import com.marvels.loanapply.customerservice.dto.UserBankDTO;
import com.marvels.loanapply.customerservice.service.ICustomerService;
import com.marvels.loanapply.dto.CollectAccountDTO;
import com.marvels.loanapply.dto.CommonConditionDTO;
import com.marvels.loanapply.dto.McfeCustomerRepaymentPlanDTO;

@Service
public class CustomerServiceImpl implements ICustomerService{
//	@Autowired
//	private CustomerServiceDao customerServiceDao;
	@Autowired
	private LoanapplyConsumerFinancialApi consumerFinancialApi;
	@Autowired
	private LoanapplyConsumerDubboApi consumerDubboApi;
	@Override
	public Map<String, Object> findPageList(CustomerServiceDTO customerDto) throws MarvelsBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("pageNum", customerDto.getPageNo());
		map.put("limit", customerDto.getPageSize());
		map.put("custIdCard", customerDto.getIdCardNo());
		map.put("mobileNum", customerDto.getPhoneNo());
		map.put("custName", customerDto.getCustomerName());
		Map<String, Object> resultMap = consumerDubboApi.queryLoanInfoByCondition(map);
		return resultMap;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<CustomerServiceDTO> findBankCardPageList(CustomerServiceDTO customerDto) throws MarvelsBusinessException {
		List<CustomerServiceDTO> customerService = new ArrayList<CustomerServiceDTO>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("idCard", customerDto.getIdCardNo());
		map.put("custIdCard", customerDto.getIdCardNo());
		Map<String, Object> result = consumerDubboApi.queryLoanInfoByCondition(map);
		List<Map<String,Object>> items = (List<Map<String, Object>>) result.get("items");
		List<UserBankDTO> list = consumerDubboApi.selectUserBankInfo(map);
		if(CollectionUtils.isEmpty(list)){
			if(result!=null && !CollectionUtils.isEmpty(items)){
				CustomerServiceDTO dto = new CustomerServiceDTO();
				dto.setCustomerName((String)items.get(0).get("custName"));
				dto.setPhoneNo((String)items.get(0).get("mobileNum"));
				dto.setIdCardNo((String)items.get(0).get("custIdCard"));
				customerService.add(dto);
			}
			return customerService;
		}
		for (UserBankDTO userBankDTO : list) {
			CustomerServiceDTO dto = new CustomerServiceDTO();
			dto.setBankCardNo(userBankDTO.getBankCardNo());
			dto.setBankCardName(userBankDTO.getBankName());
			dto.setBankCardDate(userBankDTO.getCreatedDate());
			dto.setCustomerName((String)items.get(0).get("custName"));
			dto.setPhoneNo((String)items.get(0).get("mobileNum"));
			dto.setIdCardNo((String)items.get(0).get("custIdCard"));
			customerService.add(dto);
		}
		return customerService;
	}

	@Override
	public PageInfo<McfeCustomerRepaymentPlanDTO> findPlanList(CommonConditionDTO dto) throws MarvelsBusinessException {
		return consumerFinancialApi.planList(dto);
	}

	@Override
	public CollectAccountDTO selectAccountInfo(String loanId) throws MarvelsBusinessException {
		return consumerFinancialApi.selectAccountInfo(loanId);
	}

}
